﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ThietBiSo.LinQDB;
using System.Web.Mvc;

namespace ThietBiSo.Models
{
    public class MDanhMucCon
    {
        private _24hmuaonlineDataContext context = new _24hmuaonlineDataContext();

        public List<DanhMucCon> FindAll(int id)
        {
            var v = from f in context.DanhMucCons.Where(d => d.MaDanhMuc == id) orderby f.ThuTu ascending select f;
            return v.ToList();
        }
        public List<DanhMucCon> FindAllTotal()
        {
            var v = from f in context.DanhMucCons select f;
            return v.ToList();
        }
      
        public List<DanhMucCon> FindAll_DMCon()
        {
            var v = from f in context.DanhMucCons.Where(d => d.HienThi == true) orderby f.ThuTu ascending select f;
            return v.ToList();
        }
        public int GetTotal(int madm)
        {
            return FindAllTotal().Where(d => d.MaDanhMuc == madm).Count();
        }
        public DanhMucCon GetByID(int id)
        {
            return (FindAll_DMCon().Where(d => d.MaDMCon == id)).SingleOrDefault();
        }

       
        public void Save(DanhMucCon sp)
        {
            context.DanhMucCons.InsertOnSubmit(sp);
            context.SubmitChanges();
        }
        public void Update(DanhMucCon sp)
        {
            var v = from f in context.DanhMucCons
                    select f;
            DanhMucCon spdb = v.SingleOrDefault(d => d.MaDMCon.Equals(sp.MaDMCon));
            spdb.TenDMCon = sp.TenDMCon;
            spdb.MaDMCon = sp.MaDMCon;            
            spdb.ThuTu = sp.ThuTu;
            context.SubmitChanges();
        }

        //public void Delete(int id)
        //{
        //    var v = from p in context.DanhMucCons
        //            where p.MaDMCon.Equals(id)
        //            select p;
        //    DanhMucCon spdb = v.SingleOrDefault();
        //    context.DanhMucCons.DeleteOnSubmit(spdb);
        //    context.SubmitChanges();
        //}
        public void SaveChange()
        {
        }
        //public void HienThi(int id)
        //{
        //    var v = from f in context.DanhMucCons
        //            select f;
        //    DanhMucCon spdb = v.SingleOrDefault(d => d.MaDMCon.Equals(id));
        //    if (spdb.HienThi == true)
        //    {
        //        spdb.HienThi = false;
        //    }
        //    else spdb.HienThi = true;
        //    context.SubmitChanges();
        //}
        public int GetMax()
        {
            return (from t in context.DanhMucCons
                    orderby t.MaDMCon descending
                    select t.MaDMCon).FirstOrDefault();
        }
        public int? GetMaxTT(int madm)
        {
            return (from t in context.DanhMucCons.Where(d => d.MaDanhMuc == madm)
                    orderby t.ThuTu descending
                    select t.ThuTu).FirstOrDefault();
        }
        public string Delete(int ma)
        {
            try
            {
               
                var v = from p in context.DanhMucCons
                        where p.MaDMCon.Equals(ma)
                        select p;
                DanhMucCon spdb = v.SingleOrDefault();
                context.DanhMucCons.DeleteOnSubmit(spdb);
                context.SubmitChanges();
                return null;
            }
            catch (Exception ex)
            {
                return ex.ToString();
            }
        }
        public string HienThi(int id)
        {
            try
            {
                var v = from f in context.DanhMucCons
                        select f;
                DanhMucCon lspdb = v.SingleOrDefault(d => d.MaDMCon.Equals(id));
                if (lspdb.HienThi == true)
                {
                    lspdb.HienThi = false;
                }
                else lspdb.HienThi = true;
                context.SubmitChanges();
                return null;
            }
            catch (Exception ex)
            {
                return ex.ToString();
            }
        }
      
    }
}